package com.lastproject.ejb.service;

import java.util.Properties;

import javax.ejb.Stateless;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.lastproject.service.MailSenderRemote;

@Stateless
public class MailSender implements MailSenderRemote{

	public MailSender() {

	}

	public void sendMail(String sender, String receiver, String subject, String content) {
		System.out.println("Prepare to send email.");

		try {
			Properties props = new Properties();
			props.put("mail.smtp.host", "msgc1i.msg.de");
			props.put("mail.user", sender);
			props.put("mail.password", "pass");

			Session session = Session.getInstance(props, null);
			MimeMessage msg = new MimeMessage(session);

			msg.setFrom(new InternetAddress(sender));

			msg.setRecipient(Message.RecipientType.TO, new InternetAddress(receiver));

			msg.setSubject(subject);
			msg.setText(content);
			System.out.println("Sending email.");
			Transport.send(msg);
			System.out.println("Email sent.");
		} catch (MessagingException ex) {
			ex.printStackTrace();
		}

	}
	
	
	public void sendReminderFromLibrarian(String userEmail, String bookName) {
		System.out.println("Prepare to send email.");

		try {
			Properties props = new Properties();
			props.put("mail.smtp.host", "msgc1i.msg.de");

			Session session = Session.getInstance(props, null);
			MimeMessage msg = new MimeMessage(session);
			System.out.println("email sender " + userEmail );

			msg.setFrom(new InternetAddress("library-no-reply@msg-systems.com"));

			msg.setRecipient(Message.RecipientType.TO, new InternetAddress(
					userEmail));

			msg.setSubject("Email Test");

			msg.setText("Hello! Don't forgot to bring the book " + bookName
					+ " until tomorrow! Bye");

			System.out.println("Sending email.");
			Transport.send(msg);
			System.out.println("Email sent.");
		} catch (MessagingException ex) {
			ex.printStackTrace();
		}

	}
	
	
	public void sendMail(String emails, String subject, String info) {
		// TODO Auto-generated method stub
		System.out.println("Prepare to send email.");

		try {
			Properties props = new Properties();
			props.put("mail.smtp.host", "msgc1i.msg.de");

			Session session = Session.getInstance(props, null);
			MimeMessage msg = new MimeMessage(session);

			msg.setFrom(new InternetAddress("survey-no-reply@msg-systems.com"));

			msg.setRecipient(Message.RecipientType.TO, new InternetAddress(
					emails));

			msg.setSubject(subject);
			msg.setText(info);
			System.out.println("Sending email.");
			Transport.send(msg);
			System.out.println("Email sent.");
		} catch (MessagingException ex) {
			ex.printStackTrace();
		}
	}

}
